/**
 * Clear Canvas plugin
 */
(function (namespace) {
  'use strict';

  namespace.plugins = namespace.plugins || {};

  /**
   * Clear Canvas tool
   * @param {DrawerJs.Drawer} drawerInstance
   * @param {Object} options
   * @constructor
   * @extends {DrawerJs.plugins.BaseTool}
   */
  var ClearCanvasPlugin = function ClearCanvasPluginConstructor(drawerInstance, options) {
    DrawerJs.plugins.BaseTool.call(this, drawerInstance, options);
    this.name = 'ClearCanvas';
    this.btnClass = 'btn-clear-canvas';
    this.faClass =options.faClass|| 'fa fa-trash';
    this.tooltip = drawerInstance.t('Clear Canvas');
  };

  // Inherit from BaseTool
  ClearCanvasPlugin.prototype = Object.create(DrawerJs.plugins.BaseTool.prototype);
  ClearCanvasPlugin.prototype.constructor = ClearCanvasPlugin;

  /**
   * Creates tool button with custom tooltip
   * @param {DrawerToolbar} toolbar toolbar, where this tool button will be created
   */
  ClearCanvasPlugin.prototype.createButton = function (toolbar) {
    var buttonConfig = {
      buttonOrder: this.options && this.options.buttonOrder,
      additionalClass: this.btnClass,
      iconClass: this.faClass,
      tooltipText: this.tooltip,
      clickHandler: this.onButtonClick.bind(this)
    };

    this.$toolButton = toolbar.addButton(buttonConfig);
  };

  /**
   * On tool button click - clear the canvas
   */
  ClearCanvasPlugin.prototype.onButtonClick = function () {
    if (confirm(this.drawerInstance.t('Are you sure you want to clear the canvas?'))) {
      this.drawerInstance.fCanvas.clear();
    }
  };

  namespace.plugins.ClearCanvas = ClearCanvasPlugin;
})(DrawerJs); 